/**
 * Copyright (c) 2008-2012 FeiLong, Inc. All Rights Reserved.
 * <p>
 * 	This software is the confidential and proprietary information of FeiLong Network Technology, Inc. ("Confidential Information").  <br>
 * 	You shall not disclose such Confidential Information and shall use it 
 *  only in accordance with the terms of the license agreement you entered into with FeiLong.
 * </p>
 * <p>
 * 	FeiLong MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, 
 * 	INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * 	PURPOSE, OR NON-INFRINGEMENT. <br> 
 * 	FeiLong SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
 * 	THIS SOFTWARE OR ITS DERIVATIVES.
 * </p>
 */
package com.feilong.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import loxia.support.json.JSONException;
import loxia.support.json.JSONObject;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.FieldStatsInfo;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.util.NamedList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The Class SolrUtil.
 * 
 * @author <a href="mailto:venusdrogon@163.com">金鑫</a>
 * @version 1.0 2012-6-1 下午12:39:38
 */
public final class SolrUtil{

	/** The Constant log. */
	private static final Logger	log	= LoggerFactory.getLogger(SolrUtil.class);

	private SolrUtil(){}

	/**
	 * Show solr query.
	 * 
	 * @param solrQuery
	 *            the solr query
	 */
	public static void showSolrQuery(SolrQuery solrQuery){

		if (log.isDebugEnabled()){

			Map<String, Object> map = new HashMap<String, Object>();

			// solrQuery.setShowDebugInfo(true);
			map.put("solrQuery.getFields()", solrQuery.getFields());
			map.put("solrQuery.getHighlight()", solrQuery.getHighlight());
			map.put("solrQuery.getQuery()", solrQuery.getQuery());
			map.put("solrQuery.getFacetFields()", solrQuery.getFacetFields());
			map.put("solrQuery.getFacetLimit()", solrQuery.getFacetLimit());
			map.put("solrQuery.getFacetMinCount()", solrQuery.getFacetMinCount());
			map.put("solrQuery.getFacetQuery()", solrQuery.getFacetQuery());
			map.put("solrQuery.getFacetSort()", solrQuery.getFacetSort());
			map.put("solrQuery.getFacetSortString()", solrQuery.getFacetSortString());
			map.put("solrQuery.getFilterQueries()", solrQuery.getFilterQueries());
			map.put("solrQuery.getHighlight()", solrQuery.getHighlight());
			map.put("solrQuery.getHighlightFields()", solrQuery.getHighlightFields());
			map.put("solrQuery.getHighlightFragsize()", solrQuery.getHighlightFragsize());
			map.put("solrQuery.getHighlightRequireFieldMatch()", solrQuery.getHighlightRequireFieldMatch());
			map.put("solrQuery.getHighlightSimplePost()", solrQuery.getHighlightSimplePost());
			map.put("solrQuery.getHighlightSimplePre()", solrQuery.getHighlightSimplePre());
			map.put("solrQuery.getHighlightSnippets()", solrQuery.getHighlightSnippets());
			map.put("solrQuery.getParameterNames()", solrQuery.getParameterNames());
			map.put("solrQuery.getParameterNamesIterator()", solrQuery.getParameterNamesIterator());
			map.put("solrQuery.getQueryType()", solrQuery.getQueryType());
			map.put("solrQuery.getRows()", solrQuery.getRows());
			map.put("solrQuery.getSortField()", solrQuery.getSortField());
			map.put("solrQuery.getSortFields()", solrQuery.getSortFields());
			map.put("solrQuery.getStart()", solrQuery.getStart());
			map.put("solrQuery.getTerms()", solrQuery.getTerms());
			map.put("solrQuery.getTermsFields()", solrQuery.getTermsFields());
			map.put("solrQuery.getTermsLimit()", solrQuery.getTermsLimit());
			map.put("solrQuery.getTermsLower()", solrQuery.getTermsLower());
			map.put("solrQuery.getTermsLowerInclusive()", solrQuery.getTermsLowerInclusive());
			map.put("solrQuery.getTermsMaxCount()", solrQuery.getTermsMaxCount());
			map.put("solrQuery.getTermsMinCount()", solrQuery.getTermsMinCount());
			map.put("solrQuery.getTermsPrefix()", solrQuery.getTermsPrefix());
			map.put("solrQuery.getTermsRaw()", solrQuery.getTermsRaw());
			map.put("solrQuery.getTermsRegex()", solrQuery.getTermsRegex());
			map.put("solrQuery.getTermsRegexFlags()", solrQuery.getTermsRegexFlags());
			map.put("solrQuery.getTermsSortString()", solrQuery.getTermsSortString());
			map.put("solrQuery.getTermsUpper()", solrQuery.getTermsUpper());
			map.put("solrQuery.getTermsUpperInclusive()", solrQuery.getTermsUpperInclusive());
			map.put("solrQuery.getTimeAllowed()", solrQuery.getTimeAllowed());

			// ********************************************************************

			try{
				JSONObject jsonObject = new JSONObject(map, "**,-class");
				log.debug(jsonObject.toString(4));
			}catch (JSONException e){
				e.printStackTrace();
			}
		}
	}

	/**
	 * Show query response.
	 * 
	 * @param <T>
	 *            the generic type
	 * @param queryResponse
	 *            the query response
	 * @param modelClass
	 *            the model class
	 */
	@SuppressWarnings("rawtypes")
	public static <T> void showQueryResponse(QueryResponse queryResponse,Class<T> modelClass){

		if (log.isDebugEnabled()){

			Map<String, Object> map = new HashMap<String, Object>();

			map.put("queryResponse.toString()", queryResponse.toString());

			// ********************************************************************
			int qTime = queryResponse.getQTime();
			map.put("queryResponse.getQTime()", qTime);

			String requestUrl = queryResponse.getRequestUrl();
			map.put("queryResponse.getRequestUrl()", requestUrl);

			int status = queryResponse.getStatus();
			map.put("queryResponse.getStatus()", status);

			// ********************************************************************
			NamedList<ArrayList> sortValues = queryResponse.getSortValues();
			map.put("queryResponse.getSortValues()", sortValues);

			// ********************************************************************
			Map<String, Object> debugMap = queryResponse.getDebugMap();
			map.put("queryResponse.getDebugMap()", debugMap);

			// ********************************************************************
			long elapsedTime = queryResponse.getElapsedTime();
			map.put("queryResponse.getElapsedTime()", elapsedTime);
			// ********************************************************************
			Map<String, String> explainMap = queryResponse.getExplainMap();
			map.put("queryResponse.getExplainMap()", explainMap);

			// ********************************************************************
			Map<String, FieldStatsInfo> fieldStatsInfo = queryResponse.getFieldStatsInfo();
			map.put("queryResponse.getFieldStatsInfo()", fieldStatsInfo);

			// ********************************************************************
			Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
			map.put("queryResponse.getHighlighting()", highlighting);

			// ********************************************************************
			SolrDocumentList solrDocumentList = queryResponse.getResults();

			long numFound = solrDocumentList.getNumFound();
			map.put("queryResponse.getResults().getNumFound()", numFound);

			// ********************************************************************
			map.put("modelClass.getName", modelClass.getName());

			Map<String, Integer> facetQuery = queryResponse.getFacetQuery();
			map.put("queryResponse.getFacetQuery()", facetQuery);

			List<FacetField> facetFields = queryResponse.getFacetFields();
			map.put("queryResponse.getFacetFields()", facetFields);

			List<T> beans = queryResponse.getBeans(modelClass);
			map.put("beans size", beans.size());

			try{
				JSONObject jsonObject = new JSONObject(map, "**,-class");
				log.debug(jsonObject.toString(4));
			}catch (JSONException e){
				e.printStackTrace();
			}
		}
	}
}
